package com.hightrast.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hightrast.service.api.baseEntity.exception.ServiceException;
import com.hightrast.service.api.domain.qo.rastUser.QueryRastUserListQo;
import com.hightrast.service.api.domain.qo.rastVendor.QueryRastVendorsQo;
import com.hightrast.service.api.domain.vo.rastUser.QueryRastUserInfoVo;
import com.hightrast.service.api.domain.vo.rastUser.QueryRastUserListVo;
import com.hightrast.service.api.domain.vo.rastUser.QueryUserVendorReportVo;
import com.hightrast.service.api.domain.vo.rastVendor.QueryRastVendorsVo;
import com.hightrast.service.api.entity.RastUser;
import com.hightrast.service.api.entity.RastUserRole;
import com.hightrast.service.api.enums.RoleTypeEnum;
import com.hightrast.service.mapper.RastUserMapper;
import com.hightrast.service.service.*;
import com.hightrast.service.util.SecureUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author hx
 * @since 2025-01-11
 */
@Slf4j
@Service
public class RastUserServiceImpl extends ServiceImpl<RastUserMapper, RastUser> implements IRastUserService {

    @Resource
    IRastUserBankService userBankService;
    @Resource
    IRastUserRoleService userRoleService;
    @Resource
    IRastRoleMenuService userRoleMenuService;
    @Resource
    IRastVendorService vendorService;


    /**
     * 获取用户列表，管理者权限
     * @return
     */
    @Override
    public IPage<QueryRastUserListVo> queryUserList(QueryRastUserListQo qo) {
        IPage<QueryRastUserListVo> page = new Page<>(qo.getPage(), qo.getPageSize());
        RastUser user = SecureUtil.getUser();
        // 是否存在管理者角色
        RastUserRole rastUserRole = userRoleService.queryUserRoleByUserId(user.getId()).stream().filter(a -> a.getRoleType().equals(RoleTypeEnum.ADMIN.getValue())).findFirst().orElse(null);
        if(Objects.isNull(rastUserRole)){
            return page;
//            throw new ServiceException("非管理员不能获取用户列表");
        }
        IPage<QueryRastUserListVo> pages = this.baseMapper.selectUserList(page,qo);
        // 获取这些用户对应品牌商品订单后的价格，默认是值查询 前一月的数据，因为会考虑到用户退款的抢矿，这种数据余后计算

        return pages;
    }

    /**
     * 获取当前用户信息、角色、菜单、品牌、银行账号
     *
     * @return
     */
    @Override
    public QueryRastUserInfoVo queryUserInfoRoleById(Long userId) {
        RastUser user = Optional.ofNullable(this.getById(userId)).orElseThrow(() -> new ServiceException("当前用户不存在"));
        QueryRastUserInfoVo queryRastUserInfoVo = BeanUtil.copyProperties(user, QueryRastUserInfoVo.class);
        queryRastUserInfoVo.setUserBank(userBankService.getUserBank(user.getId()).stream().findFirst().orElse(null));
        queryRastUserInfoVo.setUserRole(userRoleService.queryUserRoleByUserId(user.getId()).stream().findFirst().orElse(null));
        queryRastUserInfoVo.setMenus(userRoleMenuService.queryRastRoleMenus(user.getId()));
        QueryRastVendorsQo vendorsQo = new QueryRastVendorsQo();
        vendorsQo.setUserId(user.getId());
        QueryRastVendorsVo vendorQoVo = vendorService.queryVendors(vendorsQo).stream().findFirst().orElse(null);
        queryRastUserInfoVo.setVendor(vendorQoVo);
        return queryRastUserInfoVo;
    }

    /**
     * 删除用户 管理者权限
     * @param userId
     */
    @Override
    public void deleteUserById(Long userId) {
        RastUser user = SecureUtil.getUser();
        // 是否存在管理者角色
        RastUserRole rastUserRole = userRoleService.queryUserRoleByUserId(user.getId()).stream().filter(a -> a.getRoleType().equals(RoleTypeEnum.ADMIN.getValue())).findFirst().orElse(null);
        if(Objects.isNull(rastUserRole)){
            // 非管理员不能删除用户
            throw new ServiceException("Users cannot be deleted by non-administrators");
        }
        this.removeById(userId);
    }

    /**
     * 获取所有账号邮箱
     * @return
     */
    @Override
    public List<String> queryUserEmail() {
        RastUser user = SecureUtil.getUser();
        return this.baseMapper.selectUserEmail();
    }

    /**
     * 获取设计师关联品牌报表
     * @return
     */
    @Override
    public List<QueryUserVendorReportVo> queryUserVendorReport() {
        return this.baseMapper.selectUserVendorReport();
    }


}
